package com.cn.boot.student.controller.rbac;
import com.cn.boot.student.util.CommonResult;
import com.cn.boot.student.entity.UserEntity;
import com.cn.boot.student.service.rbac.IUserService;
import com.cn.boot.student.vo.LoginParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 用户控制层
 * @author luosir
 * Created by luosir inj 2021/02/03
 */
@RestController
@Api(tags = "用户管理")
@RequestMapping("/admin/user")
public class UserController {
    @Autowired
    private IUserService iUserService;

    /**
     *登录
     */
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public CommonResult login(LoginParams loginParams){
        return iUserService.login(loginParams);
    }

    /**
     * 注册
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public CommonResult register(UserEntity userEntity){
        return iUserService.register(userEntity);
    }
    /**
     * 用户列表、模糊查询、分页
     */
    @ApiOperation(value = "用户列表")
    @PostMapping("/findAll")
    @PreAuthorize("hasAuthority('sys:user:read')")
    public CommonResult findAll(UserEntity userEntity){
        return iUserService.findAll(userEntity);
    }

    /**
     * 学员列表、模糊查询、分页
     */
    @ApiOperation(value = "用户列表")
    @PostMapping("/getStudent")
    @PreAuthorize("hasAuthority('sys:user:read')")
    public CommonResult getStudent(UserEntity userEntity){
        return iUserService.getStudent(userEntity);
    }
    /**
     * 根据id查找数据
     */
    @ApiOperation(value = "根据id查找用户")
    @GetMapping("/findById")
    @PreAuthorize("hasAuthority('sys:user:read')")
    public CommonResult findById(Integer id){
        return iUserService.findById(id);
    }
    /**
     * 单条数据删除
     */
    @ApiOperation(value = "单条数据删除")
    @Delete("/del")
    @PreAuthorize("hasAuthority('sys:user:del')")
    public CommonResult del( Integer id){
        return iUserService.del(id);
    }
    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除")
    @Delete("/dels")
    @PreAuthorize("hasAuthority('sys:user:del')")
    public CommonResult dels( List<Integer> ids){
        return iUserService.dels(ids);
    }
    /**
     * 更新
     */
    @ApiOperation(value = "更新数据")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public CommonResult update( UserEntity userEntity){
        return iUserService.update(userEntity);
    }
    /**
     * 添加用户
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("/add")
//    @PreAuthorize("hasAuthority('sys:user:add')")
    public CommonResult add( UserEntity userEntity){
        return iUserService.add(userEntity);
    }
    /**
     * 批量添加
     */
    @ApiOperation(value = "批量添加")
    @PostMapping("/adds")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public CommonResult adds(List<UserEntity> users){
        return iUserService.adds(users);
    }
    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/updatePassword")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public CommonResult updatePassword(UserEntity userEntity){
        return iUserService.updatePassword(userEntity);
    }
    /**
     * 根据用户名查询用户id
     */
    @ApiOperation(value = "查找用户id")
    @PostMapping("/findUserIdByUsername")
    public CommonResult findUserIdByUsername(UserEntity userEntity){
        return iUserService.findUserIdByUsername(userEntity.getUsername());

    }

}
